﻿using System;
using DataModelException = System.Qizx.Api.DataModelException;
using EvaluationException = System.Qizx.Api.EvaluationException;
using BasicNode = System.Qizx.Xdm.BasicNode;
using EvalContext = System.Qizx.XQuery.EvalContext;
using Focus = System.Qizx.XQuery.Focus;
using XQValue = System.Qizx.XQuery.XQValue;
using System.Qizx.XQuery.Dt;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Qizx.XQuery.Op {
    public class TypeCaseClause : VarClause {
        public TypeCaseClause()
            : base(null) {
            declaredType = XQType.ANY;
        }

        public void dump(ExprDisplay d) {
            d.header(this);
            d.property("variable", variable);
            d.property("varAddress", varDecl != null ? varDecl.address : -1);
            d.property("varType", declaredType);
            d.child("return", expr);
        }

        public override Expression StaticCheck(ModuleContext context, int flags) {
            LocalVariable mark = context.latestLocalVariable();
            if (variable != null) {
                varDecl =
                    context.defineLocalVariable(variable, declaredType, this);
                varDecl.storageType(declaredType, context);
            }
            expr = context.simpleStaticCheck(expr, 0);

            updating = UpdatingExpr.isUpdating(expr);

            if (variable != null)
                context.popLocalVariables(mark);
            type = expr.getType();
            return this;
        }
    }
}